package com.chinaums.xm.wechat.service.wechat;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Optional;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.chinaums.xm.wechat.WeixinConstant;
import com.chinaums.xm.wechat.domain.wechat.AccessToken;
import com.chinaums.xm.wechat.domain.wechat.account.ActionInfo;
import com.chinaums.xm.wechat.domain.wechat.account.Scene;
import com.chinaums.xm.wechat.domain.wechat.account.Ticket;
import com.chinaums.xm.wechat.service.Communicator;

import net.sf.json.JSONObject;

/**
 * 账号管理service
 * @author chenjf
 *
 */
@Service
public class AccountService {

	private static final Logger logger = LoggerFactory.getLogger(Communicator.class);
	
	@Resource
	private Communicator communicator;
	
	@Resource
	private CommonService commonService;
	
	/**
	 * 创建临时二维码
	 * @param expire_seconds
	 * @param action_name
	 */
	public void createTempTicket(final String expire_seconds,final String action_name){
		final Optional<AccessToken> accessToken = commonService.getAccessToken();
		if(accessToken.isPresent()){
			final String url = WeixinConstant.TEMP_TICKET_CREATE_URL.replace("TOKENPOST", accessToken.get().getToken());
			final Ticket ticket = new Ticket();
			ticket.setExpire_seconds(expire_seconds);
			ticket.setAction_name(action_name);
			ActionInfo actionInfo = new ActionInfo();
			Scene scene = new Scene();
			scene.setScene_id("123");
			actionInfo.setScene(scene);
			ticket.setAction_info(actionInfo);
			final JSONObject request = JSONObject.fromObject(ticket);
			final Optional<String> responseOpt = communicator.postRequest(url, String.valueOf(request));
			final String msg = responseOpt.get();
			logger.info("response is:{}",msg);
		}
	}
	
	/**
	 * 创建永久二维码
	 * @param action_name
	 */
	public void createForeverTicket(final String action_name){
		final Optional<AccessToken> accessToken = commonService.getAccessToken();
		if(accessToken.isPresent()){
			final String url = WeixinConstant.TEMP_TICKET_CREATE_URL.replace("TOKENPOST", accessToken.get().getToken());
			final Ticket ticket = new Ticket();
			ticket.setAction_name(action_name);
			final ActionInfo actionInfo = new ActionInfo();
			final Scene scene = new Scene();
			scene.setScene_id("123");
			actionInfo.setScene(scene);
			ticket.setAction_info(actionInfo);
			final JSONObject request = JSONObject.fromObject(ticket);
			final Optional<String> responseOpt = communicator.postRequest(url, String.valueOf(request));
			final String msg = responseOpt.get();
			logger.info("response is:{}",msg);
		}
	}
	
	/**
	 * 下载临时二维码
	 * @param ticket
	 * @throws UnsupportedEncodingException
	 */
	public void getTempTicket(final String ticket) throws UnsupportedEncodingException{
		final String url = WeixinConstant.TEMP_TICKET_GET_URL.replace("TICKET", URLEncoder.encode(ticket,"UTF-8"));
		communicator.getImageRequest(url,"d:\\tempTicket.jpg");
	}
	

	/**
	 * 下载永久二维码
	 * @param ticket
	 * @throws UnsupportedEncodingException
	 */
	public void getForeverTicket(final String ticket) throws UnsupportedEncodingException{
		final String url = WeixinConstant.TEMP_TICKET_GET_URL.replace("TICKET", URLEncoder.encode(ticket,"UTF-8"));
		communicator.getImageRequest(url,"d:\\foreverTicket.jpg");
	}
}
